package one.stand.controller;

import com.jmp.api.admin.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.feign.AppIndexGoodsFeign;
import com.jmp.feign.BannerFeign;
import com.jmp.service.api.admin.BannerApi;
import com.jmp.service.api.admin.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.Company2Dto;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.GoodsApiAdminMapper;
import one.stand.model.Goods;
import one.stand.model.GoodsType;
import one.stand.model.ResultModel;
import one.stand.service.*;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author cjr
 */
@Api(description = "后台用户管理")
@RestController
@RequestMapping("adminApi/app/config")
@Slf4j
public class AppConfigController {
    @Autowired
    private AppIndexGoodsFeign appIndexGoodsFeign;
    @Autowired
    private BannerFeign bannerFeign;

    @Autowired
    private GoodsBaseService goodsBaseService;
    @Autowired
    private GoodsPhotoService goodsPhotoService;
    @Autowired
    private CompanyBaseService companyBaseService;

    @Autowired
    private GoodsApiAdminMapper goodsApiAdminMapper;

    @ApiOperation("轮播图列表")
    @GetMapping("banner")
    @SecurityAuthCheck
    public ResultModel<BannerResponse> list(BannerRequest request) {

        BannerListDto bannerListDto = BannerListDto.builder()
                .companyId(BannerApi.COMPANY_ID_PLATFORM)
                .bannerType(request.getBannerType())
                .refId(request.getRefId())
                .build();
        bannerListDto.setAll();
        ResultModel<BannerListVo> bannerListVoResultModel = bannerFeign.list(bannerListDto);
        AssertUtil.isTrue(bannerListVoResultModel);
        List<BannerResponse.Banner> banners = ConvertUtils.list(bannerListVoResultModel.getData().getBannerPage().getList(), companyBanner -> {
            BannerResponse.Banner banner = BannerResponse.Banner.builder()
                    .id(companyBanner.getId())
                    .companyId(companyBanner.getCompanyId())
                    .source(companyBanner.getSource())
                    .type(companyBanner.getType())
                    .event(companyBanner.getEvent())
                    .eventId(companyBanner.getEventId())
                    .memo(companyBanner.getMemo())
                    .sort(companyBanner.getSort())
                    .refId(companyBanner.getRefId())
                    .build();

            if (BannerApi.EVENT_GOODS == banner.getEvent()) {
                Goods goodsModel = goodsBaseService.get(Integer.valueOf(companyBanner.getEventId()));
                if (Objects.nonNull(goodsModel)) {
                    banner.setEventIdName(goodsModel.getGoodsName());
                }
            } else if (BannerApi.EVENT_COMPANY == banner.getEvent()) {
                Optional<Company2Dto> company = companyBaseService.get(Integer.valueOf(banner.getEventId()));
                banner.setEventIdName(company.map(Company2Dto::getCompanyName).orElse(null));
            } else {
                banner.setEventIdName(banner.getEventId());
            }

            return banner;
        });

        BannerResponse response = new BannerResponse();
        response.setBanners(banners);
        return ResultModel.success(response);
    }

    @ApiOperation("添加或更新轮播图")
    @PostMapping("banner/edit")
    @SecurityAuthCheck
    public ResultModel edit(@RequestBody @Valid BannerEditRequest request) {

        ResultModel resultModel = bannerFeign.save(BannerSaveDto.builder()
                .id(request.getId())
                .bannerType(request.getBannerType())
                .companyId(BannerApi.COMPANY_ID_PLATFORM)
                .source(request.getSource())
                .type(request.getType())
                .event(request.getEvent())
                .eventId(request.getEventId())
                .sort(request.getSort())
                .memo(request.getMemo())
                .refId(request.getRefId())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation("删除轮播图")
    @PostMapping("banner/remove")
    @SecurityAuthCheck
    public ResultModel remove(@RequestBody BannerRemoveRequest request) {

        bannerFeign.remove(BannerRemoveDto.builder().id(request.getId()).build());

        return ResultModel.success();
    }

    @ApiOperation("商品:列表")
    @GetMapping("/goods")
    @SecurityAuthCheck
    public ResultModel<AppConfigGoodsResponse> goods(AppConfigGoodsRequest request) {

        AppIndexGoodsRequest goodsRequest = AppIndexGoodsRequest.builder()
                .type(request.getType())
                .refId(request.getRefId())
                .build();
        goodsRequest.setPageNum(request.getPageNum());
        goodsRequest.setPageSize(request.getPageSize());
        ResultModel<AppIndexGoodsResponse> responseResultModel = appIndexGoodsFeign.list(goodsRequest);
        AssertUtil.isTrue(responseResultModel);

        PageResponse2<AppConfigGoodsResponse.Goods> goodsList = ConvertUtils.page2(responseResultModel.getData().getGoodsList(), goods -> {
            Goods goods1 = goodsBaseService.get(goods.getGoodsId());
            Optional<Company2Dto> company = companyBaseService.get(goods1.getCompanyId());
            return AppConfigGoodsResponse.Goods.builder()
                    .id(goods.getId())
                    .sort(goods.getSort())
                    .goodsId(goods.getGoodsId())
                    .goodsName(goods1.getGoodsName())
                    .companyName(company.map(Company2Dto::getCompanyName).orElse(null))
                    .photo(goodsPhotoService.getPath(goods.getGoodsId()))
                    .refId(goods.getRefId())
                    .build();
        });

        List<GoodsType> list = goodsApiAdminMapper.selectSelectTypeList(GoodsTypeService.PLATFORM_COMPANY_ID, false);
        List<AppConfigGoodsResponse.Type> types = ConvertUtils.list(list, type -> AppConfigGoodsResponse.Type.builder()
                .goodstypeId(type.getGoodstypeId())
                .name(type.getName())
                .parentId(type.getParentId())
                .groupName(type.getGroupName())
                .build());

        AppConfigGoodsResponse response = AppConfigGoodsResponse.builder()
                .goodsList(goodsList)
                .typeList(types)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("商品:添加或更新")
    @PostMapping("goods/edit")
    @SecurityAuthCheck
    public ResultModel goodsEdit(@RequestBody @Valid AppConfigGoodsSaveRequest request) {

        AppIndexGoodsSaveRequest saveRequest = AppIndexGoodsSaveRequest.builder()
                .id(request.getId())
                .goodsId(request.getGoodsId())
                .sort(request.getSort())
                .type(request.getType())
                .refId(request.getRefId())
                .build();
        ResultModel resultModel = appIndexGoodsFeign.appIndexGoodsSave(saveRequest);
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation("商品:删除")
    @PostMapping("goods/remove")
    @SecurityAuthCheck
    public ResultModel goodsRemove(@RequestBody BannerRemoveRequest request) {

        ResultModel resultModel = appIndexGoodsFeign.appIndexGoodsRemove(AppIndexGoodsRemoveRequest.builder().id(request.getId()).build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }
}
